package com.angel.web.health;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response.Status.Family;

import org.apache.log4j.chainsaw.Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.base.PageData;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.JsonResult;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.JsonUtils;
import com.angel.core.util.Pager;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzFamily;
import com.angel.provider.health.BuzFamilyProvider;

import net.sf.json.JSONArray;

/**
 * 家庭管理
 * @author cheng sir
 *
 */
@Controller
@RequestMapping("family")
public class BuzFamilyController {
	
	@Autowired
	BuzFamilyProvider familyService;

	@RequestMapping("index")
	public String index(HttpServletRequest request,BuzFamily buzFamily,Pager pager){
		buzFamily.setEnable(1);
		pager = familyService.findAll(buzFamily,pager);
		request.setAttribute("re", buzFamily.getSearchVal());
		request.setAttribute("data", pager);
		return "member/vip_family";
	}
	
	/**
	 * 删除用户
	 */
	@ResponseBody
	@RequestMapping(value="/del")
	public String del(String ids){
		try {
			String[] idlist =ids.split(","); 
			if(idlist!=null){
				BuzFamily b = null;
				for(int i=0;i<idlist.length;i++){
					b = new BuzFamily();
					b.setId(Integer.parseInt(idlist[i]));
					familyService.delById(b);
				}
			}
			return JsonUtil.toJson(new JsonEntity("200",JsonUtil.toJson(0)));
		} catch (Exception e) {
			return JsonUtil.toJson(new JsonEntity("-1", "修改失败"));
		}
	}
	
	/**
	 * 跳转添加页面
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(){
		return "member/vip_family_add";
	}
	
	/**
	 * 跳转修改/查看页面
	 * @return
	 */
	@RequestMapping("/toLook/{id}/{k}/{m}")
	public String toLook(HttpServletRequest request,@PathVariable("id")Integer id,@PathVariable("k")String k,@PathVariable("m")String m){
		BuzFamily  f = familyService.getById(id);
		JSONArray ar = JSONArray.fromObject(f.getFamilyPeople());
		
		request.setAttribute("re", f);
		request.setAttribute("k", k);
		request.setAttribute("m", m);
		request.setAttribute("familyPeople", ar);
		return "member/vip_family_look";
	}
	
	/**
	 * 
	 * @param request
	 * @param buzFamily
	 * @return
	 */
	@RequestMapping("/save")
	public String save(HttpServletRequest request,@ModelAttribute("buzFamily") BuzFamily buzFamily){
		String username = (String) request.getSession().getAttribute("user");
		buzFamily.setCreateBy(username);
		buzFamily.setUpdateBy(username);
		int i = familyService.insert(buzFamily);
		
		BuzFamily e = new BuzFamily();
		e.setCode(CodeUtil.createCode(null, Constants.CUSTOMERFANMILYNUM, buzFamily.getId()));
		e.setId(buzFamily.getId());
		familyService.updateById(e);
		
		return "redirect:/family/index";
	}
	
	@RequestMapping("/update")
	public String update(BuzFamily buzFamily,HttpServletRequest request){
		int i = familyService.updateById(buzFamily);
		if(i==1){
			return "redirect:/family/toLook/"+buzFamily.getId()+"/2/success";
		}
		return "redirect:/family/toLook/"+buzFamily.getId()+"/2/error";
	}
	
	/**
	 * ajax加载用户信息
	 * @param serach
	 */
	@ResponseBody
	@RequestMapping("/loadUser")
	public void loadUser(HttpServletResponse response, String search){
		JsonResult.successResult(response, JsonUtils.toJson(familyService.queryCustomer(search)));
	}
	
	/**
	 * ajax加载医师信息
	 * @param serach
	 */
	@ResponseBody
	@RequestMapping("/loadDoctor")
	public void loadDoctor(HttpServletResponse response, String search){
		JsonResult.successResult(response, JsonUtils.toJson(familyService.queryDoctor(search)));
	}
	
	
	public static void main(String[] args) {
		String json = "[{\"a\":\"111\",\"b\":\"222\",\"c\":\"333\"},{\"a\":\"1000\",\"b\":\"2000\",\"c\":\"000\"},{\"a\":\"999\",\"b\":\"300\",\"c\":\"700\"}]";
        JSONArray jsonArr = JSONArray.fromObject(json);
        String a[] = new String[jsonArr.size()];
        String b[] = new String[jsonArr.size()];
        String c[] = new String[jsonArr.size()];
        for (int i = 0; i < jsonArr.size(); i++) {
            a[i] = jsonArr.getJSONObject(i).getString("a");
            b[i] = jsonArr.getJSONObject(i).getString("b");
            c[i] = jsonArr.getJSONObject(i).getString("c");
       }
         
        for (int i = 0; i < c.length; i++) {
           System.out.print(a[i]+" ");
           System.out.print(b[i]+" ");
           System.out.print(c[i]);
           System.out.println();
       }
	}
}
